#include <algorithm>
#include <climits>
#include <ios>
#include <iostream>
using namespace std;

const int MAX_T = 1000+5,MAX_FH=30+5,MAX_G=100+5;
int D,G;
struct Rubbish{
    int T,F,H;
}rub[MAX_G];
int dp[MAX_G];

int main(){
    ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    dp[0]=10;
    cin>>D>>G;
    for(int i=1;i<=G;i++){
        cin>>rub[i].T>>rub[i].F>>rub[i].H;
    }
    sort(rub+1,rub+1+G,[](Rubbish a,Rubbish b)->bool{
        return a.T<b.T;
    });
    for(int i=1;i<=G;i++){
        for(int j=D;j>=0;j--){
            if(rub[i].T>dp[j])continue;
            if(j+rub[i].H>=D){
                cout<<rub[i].T<<endl;
                return 0;
            }
            dp[j+rub[i].H] = max(dp[j],dp[j+rub[i].H]);
            dp[j]=dp[j]+rub[i].F;
        }
    }
    cout<<dp[0]<<endl;
}